/**
 * 
 */
package scriptrunner.execution;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * @author Administrator
 * 
 */
public class LHListName implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2403223027471181533L;

	/**
	 * @param n
	 */
	public LHListName(String n) {
		listAccess = new ArrayList<EvaluateExpr>();
		this.n = n.substring(0);
	}

	/**
	 * @param ee
	 */
	public void AddAccessParameter(EvaluateExpr ee) {
		listAccess.add(ee);
	}

	/**
	 * @param se
	 * @return
	 * @throws ExecuteErrorException
	 */
	public ValueListObject GetLHObject(Session se) throws ExecuteErrorException {
		ValueObject v = se.getVariableValue(n);

		for (EvaluateExpr ee : listAccess) {
			if (v instanceof ValueListObject) {
				ValueListObject listValue = (ValueListObject) v;
				ValueObject idx = ee.CalculateResult(se);
				v = listValue.GetListElement(idx);
			} else
				throw new ExecuteErrorException("The value is not a list");
		}

		if (v instanceof ValueListObject)
			return (ValueListObject) v;
		else
			throw new ExecuteErrorException("The value is not a list");
	}

	private ArrayList<EvaluateExpr> listAccess;
	String n;
}
